package com.zipking.cloud.springbootmybatis.controller;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public class AsyncTasks {

    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    public AsyncTasks(ThreadPoolTaskExecutor threadPoolTaskExecutor) {
        this.threadPoolTaskExecutor = threadPoolTaskExecutor;
    }

    public List<Future<String>> invokeAllTasks() throws InterruptedException {
        List<Callable<String>> callableTasks = new ArrayList<>();
        callableTasks.add(new Callable<String>() {
            @Override
            public String call() throws Exception {
                // 任务内容
                return "Task 1 result";
            }
        });
        callableTasks.add(new Callable<String>() {
            @Override
            public String call() throws Exception {
                // 任务内容
                return "Task 2 result";
            }
        });

        // 提交所有任务并等待完成
        return threadPoolTaskExecutor.getThreadPoolExecutor().invokeAll(callableTasks);
    }
}
